#!/usr/bin/env bash


# ----------------------------------------------------------------------
# Filename	:  tty
# Version	:  1.0
# Date		:  2022/02/22
# Author	:  Lz
# Email		:  liuzuo@kylinos.com.cn 
# History	:     
#            	   Version 1.0, 2022/02/22
# Function	:  串口设备限制
# Out		:        
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------


# 测试主题
Title_Env_LTFLIB="安全加固(接入类) - 串口设备限制"


## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB(){
	if [ "Z${TTY_Access_SSRFlag}" == "Z" ];then
		OutputRet_LTFLIB "${ERROR}"	
		TestRetParse_LTFLIB "未定义变量 TTY_Access_SSRFlag"
	elif [ "Z${TTY_Access_SSRFlag}" != "ZTrue" -a "Z${TTY_Access_SSRFlag}" != "ZFalse" ];then
		OutputRet_LTFLIB "${ERROR}"	
		TestRetParse_LTFLIB "TTY_Access_SSRFlag 无效的参数: ${TTY_Access_SSRFlag}"
	fi

        return $TPASS
}


## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB(){
        return $TPASS
}


## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB(){
        testcase_1

        return $TPASS
}


# 功能测试
testcase_1(){
        local cmd="sudo setserial -g /dev/ttyS0"
	local flag="^unknown,$"

        local files_conf=$(ls /dev/ttyS*)
	local file_conf=""
	for file_conf in ${files_conf[@]}
	do
		if [ "Z${TTY_Access_SSRFlag}" == "ZTrue" ];then
		# 允许使用串口设备
			eval ${cmd}
        	        eval ${cmd} | awk '{print $3}' | grep -q "${flag}"
	                CommRetParse_LTFLIB "${cmd}查询,要求当前串口状态不为${flag}" "False" "yes"
		else
		# 不允许使用串口设备
			eval ${cmd}
        	        eval ${cmd} | awk '{print $3}' | grep -q "${flag}"
	                CommRetParse_LTFLIB "${cmd}查询,要求当前串口状态为${flag}" "False"
		fi
	done
}

## 文件内容测试,old
#testcase_1(){
#        local flag_str="KERNEL==\"ttyS\[0-9\]\*\", SUBSYSTEM==\"tty\", MODE=\"0000\""
#        local false_str="${flag_str}"
#        local true_str="${flag_str}"
#
#        # 查看文件中配置
#        local file_conf="/etc/udev/rules.d/90-ssr-external.rules"
#        local cmd="cat ${file_conf}"
#
#        if [ "Z${TTY_Access_SSRFlag}" == "ZTrue" ];then
#                eval ${cmd} | grep -E "${flag_str}"
#                eval ${cmd} | grep -Eq "${true_str}"
#                CommRetParse_LTFLIB "${cmd}查询,要求不存在${true_str}" "False" "yes"
#        else
#                eval ${cmd} | grep -E "${flag_str}"
#                eval ${cmd} | grep -Eq "${true_str}"
#                CommRetParse_LTFLIB "${cmd}查询,要求存在${true_str}" "False"
#        fi
#}


#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
